/*     */ package com.zimbra.cs.account;
/*     */ 
/*     */ import com.zimbra.common.account.ZAttrProvisioning.AccountStatus;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.index.SortBy;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.Mountpoint;
/*     */ import java.util.Date;
/*     */ import java.util.LinkedList;
/*     */ import java.util.List;
/*     */ import java.util.TimerTask;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ExternalAccountManagerTask
/*     */   extends TimerTask
/*     */ {
/*     */   public void run()
/*     */   {
/*  45 */     ZimbraLog.misc.info("Starting external virtual account status manager task");
/*     */     
/*  47 */     Provisioning prov = Provisioning.getInstance();
/*  48 */     SearchAccountsOptions searchOpts = new SearchAccountsOptions();
/*     */     try {
/*  50 */       searchOpts.setFilter(ZLdapFilterFactory.getInstance().externalAccountsHomedOnServer(prov.getLocalServer().getServiceHostname()));
/*     */       
/*  52 */       List<NamedEntry> accounts = prov.searchDirectory(searchOpts);
/*     */       
/*  54 */       for (NamedEntry ne : accounts) {
/*  55 */         Account account = (Account)ne;
/*  56 */         Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
/*  57 */         if (mbox == null) {
/*  58 */           ZimbraLog.misc.error("Mailbox for external virtual account %s is unexpectedly null", new Object[] { account.getId() });
/*     */         }
/*     */         else
/*     */         {
/*  62 */           List<Folder> folders = mbox.getFolderList(null, SortBy.NONE);
/*  63 */           List<Mountpoint> mountpoints = new LinkedList();
/*  64 */           for (Folder folder : folders) {
/*  65 */             if ((folder instanceof Mountpoint)) {
/*  66 */               mountpoints.add((Mountpoint)folder);
/*     */             }
/*     */           }
/*  69 */           if (mountpoints.isEmpty()) {
/*  70 */             disableOrDeleteAccount(prov, account, mbox);
/*     */           }
/*     */           else
/*     */           {
/*  74 */             ShareInfoVisitor shareInfoVisitor = new ShareInfoVisitor(mountpoints);
/*  75 */             ShareInfo.Published.get(prov, account, (byte)7, null, shareInfoVisitor);
/*  76 */             boolean hasValidMountpoint = shareInfoVisitor.getResult();
/*     */             
/*  78 */             if (!hasValidMountpoint) {
/*  79 */               disableOrDeleteAccount(prov, account, mbox);
/*  80 */             } else if (account.getAccountStatus() == ZAttrProvisioning.AccountStatus.closed)
/*     */             {
/*  82 */               account.setAccountStatus(ZAttrProvisioning.AccountStatus.active); }
/*     */           }
/*     */         }
/*     */       }
/*  86 */     } catch (ServiceException e) { ZimbraLog.misc.warn("Error during external virtual account status manager task run", e);
/*     */     }
/*  88 */     ZimbraLog.misc.info("Finished external virtual account status manager task");
/*     */   }
/*     */   
/*     */   private static void disableOrDeleteAccount(Provisioning prov, Account account, Mailbox mbox) throws ServiceException
/*     */   {
/*  93 */     ZAttrProvisioning.AccountStatus accountStatus = account.getAccountStatus();
/*  94 */     if (accountStatus == ZAttrProvisioning.AccountStatus.active) {
/*  95 */       account.setExternalAccountDisabledTime(new Date());
/*  96 */       account.setAccountStatus(ZAttrProvisioning.AccountStatus.closed);
/*     */     } else {
/*  98 */       long disabledAcctLifetime = account.getExternalAccountLifetimeAfterDisabled();
/*  99 */       if ((accountStatus == ZAttrProvisioning.AccountStatus.closed) && (disabledAcctLifetime != 0L)) {
/* 100 */         Date timeWhenDisabled = account.getExternalAccountDisabledTime();
/* 101 */         if ((timeWhenDisabled != null) && (System.currentTimeMillis() - timeWhenDisabled.getTime() > disabledAcctLifetime))
/*     */         {
/* 103 */           mbox.deleteMailbox();
/* 104 */           prov.deleteAccount(account.getId());
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static class ShareInfoVisitor implements Provisioning.PublishedShareInfoVisitor
/*     */   {
/*     */     private List<Mountpoint> mountpoints;
/* 113 */     private boolean result = false;
/*     */     
/*     */     public ShareInfoVisitor(List<Mountpoint> mountpoints) {
/* 116 */       this.mountpoints = mountpoints;
/*     */     }
/*     */     
/*     */     public void visit(ShareInfoData shareInfoData) throws ServiceException
/*     */     {
/* 121 */       for (Mountpoint mountpoint : this.mountpoints) {
/* 122 */         if ((mountpoint.getOwnerId().equals(shareInfoData.getOwnerAcctId())) && (mountpoint.getRemoteId() == shareInfoData.getItemId()))
/*     */         {
/* 124 */           this.result = true;
/* 125 */           break;
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     public boolean getResult() {
/* 131 */       return this.result;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ExternalAccountManagerTask.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */